/*
 * @Author: Simon 1958058172@qq.com
 * @Date: 2025-07-17 17:01:51
 * @LastEditors: Simon 1958058172@qq.com
 * @LastEditTime: 2025-07-18 09:09:51
 * @FilePath: \java.1212\day01\Main.java
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE99 
 */
package day01;

import java.lang.String;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;



public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        sc.nextLine(); // 处理换行
        String[] loves = sc.nextLine().split(" ");
        String s = sc.nextLine();

        // 预处理：记录每个偏爱字符的位置
        List<Integer>[] charPos = new List[m];
        for (int i = 0; i < m; i++) {
            charPos[i] = new ArrayList<>();
        }
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            for (int j = 0; j < m; j++) {
                if (loves[j].charAt(0) == c) {
                    charPos[j].add(i);
                    break;
                }
            }
        }

        StringBuilder result = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char current = s.charAt(i);
            boolean isLove = false;
            for (String love : loves) {
                if (love.charAt(0) == current) {
                    isLove = true;
                    break;
                }
            }
            if (isLove) {
                result.append(current);
                continue;
            }

            // 找最近的偏爱字符
            int minDist = Integer.MAX_VALUE;
            char nearestChar = ' ';
            for (int j = 0; j < m; j++) {
                List<Integer> posList = charPos[j];
                if (posList.isEmpty()) continue;

                // 二分查找
                int left = 0, right = posList.size() - 1;
                while (left < right) {
                    int mid = (left + right) / 2;
                    if (posList.get(mid) < i) {
                        left = mid + 1;
                    } else {
                        right = mid;
                    }
                }

                // 检查 left 和 left-1 位置
                for (int k = left; k >= Math.max(0, left - 1); k--) {
                    int dist = Math.abs(posList.get(k) - i);
                    if (dist < minDist || (dist == minDist && loves[j].charAt(0) < nearestChar)) {
                        minDist = dist;
                        nearestChar = loves[j].charAt(0);
                    }
                }
            }
            result.append(nearestChar);
        }

        System.out.println(result.toString());
    }
}

